package cn.kgc.ssm.controller;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.dto.UserPageDTO;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mosin
 * @version: v1.0  2022/12/14
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public ResponseResult  selectUserPage(UserPageDTO userPageDTO){
        ResponseResult responseResult  = userService.getPage(userPageDTO);
        return  responseResult ;
    }

    @RequestMapping("add")
    public ResponseResult  addUser(@RequestBody User user){
        user.setPassword(SecureUtil.md5("888888"));
        System.out.println("user = " + user);
        ResponseResult responseResult  = userService.addUser(user);
        return  responseResult ;
    }

    @RequestMapping("getById")
    public User  getById(Integer userId){

        return  userService.getById(userId);
    }

    @RequestMapping("edit")
    public ResponseResult  editUser(@RequestBody User user){

        return  userService.editUser(user);
    }

    //更新用户状态
     @RequestMapping("updateStatus")
     public  ResponseResult  updateStatus(Integer userId,Integer status){
         System.out.println("userId = " + userId);
         System.out.println("status = " + status);
         ResponseResult responseResult =  userService.updateStatus(userId,status);
        return  responseResult;
     }


    @RequestMapping("delete")
    public ResponseResult  deleteByIds(String ids){
       ResponseResult responseResult =  userService.deleteByIds(ids);
        return responseResult;
    }


    @RequestMapping("login")
    public ResponseResult login(String username,String password, String captcha,@SessionAttribute("code") String code){
        ResponseResult responseResult = new ResponseResult();
        if(code.equals(captcha)){
            responseResult =  userService.login(username,password);
        }else{
            responseResult.setMsg("验证码不正确");
            responseResult.setCode(500);
        }

        return responseResult ;
    }

}
